package com.aiYouPms.admin.config;

import com.aiYouPms.common.api.HttpResult;
import jakarta.validation.ValidationException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DataAccessException;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.validation.method.MethodValidationException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

/**
 * 全局异常处理器
 * 用于捕获和处理系统中未被处理的异常
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 处理通用异常
     *
     * @param e 异常对象
     * @return 错误响应
     */
    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public HttpResult<String> handleException(Exception e) {
        log.error("系统异常-101", e);
        return HttpResult.error("错误: " + e.getMessage());
    }

    @ExceptionHandler(BindException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public  HttpResult<Map<String, String>> handleBindException(BindException e) {
        Map<String, String> errors = new HashMap<>();
        e.getBindingResult().getAllErrors().forEach((error) -> {
            String fieldName = ((FieldError) error).getField();
            String errorMessage = error.getDefaultMessage();
            errors.put(fieldName, errorMessage);
        });

        return HttpResult.error("请求参数有误", errors);
    }

    /**
     * 处理运行时异常
     *
     * @param e 运行时异常对象
     * @return 错误响应
     */
    @ExceptionHandler(RuntimeException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public HttpResult<String> handleRuntimeException(RuntimeException e) {
        log.error("运行时异常-102", e);
        return HttpResult.error("错误: " + e.getMessage());
    }

    /**
     * 处理空指针异常
     *
     * @param e 空指针异常对象
     * @return 错误响应
     */
    @ExceptionHandler(NullPointerException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public HttpResult<String> handleNullPointerException(NullPointerException e) {
        log.error("空指针-103", e);
        return HttpResult.error("空指针错误，请联系管理员");
    }

    /**
     * 处理数据库访问异常
     *
     * @param e 数据库访问异常对象
     * @return 错误响应
     */
    @ExceptionHandler(DataAccessException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public HttpResult<String> handleDataAccessException(DataAccessException e) {
        log.error("数据库异常-104", e);
        return HttpResult.error("运行时异常-104，请稍后重试");
    }

    /**
     * 处理SQL异常
     *
     * @param e SQL异常对象
     * @return 错误响应
     */
    @ExceptionHandler(SQLException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public HttpResult<String> handleSQLException(SQLException e) {
        log.error("SQL异常-105", e);
        return HttpResult.error("运行时异常-105，请稍后重试");
    }

    /**
     * 处理非法参数异常
     *
     * @param e 非法参数异常对象
     * @return 错误响应
     */
    @ExceptionHandler(IllegalArgumentException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public HttpResult<String> handleIllegalArgumentException(IllegalArgumentException e) {
        log.error("非法参数异常", e);
        return HttpResult.error("请求参数错误: " + e.getMessage());
    }
}
